package com.zx.service.impl;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.zx.service.wxService;



@Service
public class wxServiceImp implements wxService {
//	private Logger logger=Logger.getLogger(wxServiceImp.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	public Map<String, Object> getAccess_token(String appID,String appsecret){
      List<Map<String, Object>> resultList=jdbcTemplate.queryForList("select name,value,update_time from wx_base where appID=? AND appsecret=? AND NAME=?", new Object[]{appID,appsecret,"access_token"}); 
      if(resultList.size()>0){
    	  return resultList.get(0);
      }
      else
      {
    	  return null;
      }	  
	}
	public void setAccess_token(String access_token,String appID,String appsecret){
		jdbcTemplate.update("INSERT INTO wx_base (appID,appsecret,value,name) VALUES(?,?,?,?)", new Object[]{appID,appsecret,access_token,"access_token"});
	}
	public void updateAccess_token(String access_token,String appID,String appsecret){
		jdbcTemplate.update("UPDATE wx_base SET value=?,update_time=NOW() WHERE appID=? AND appsecret=? and name=?", new Object[]{access_token,appID,appsecret,"access_token"});
	}

	
	
	@Override
	public Map<String,Object> getApi_ticket(String appID, String appsecret) {
		List<Map<String,Object>> resultList=jdbcTemplate.queryForList("select name,value,update_time from wx_base where appID=? AND appsecret=? AND NAME=?", new Object[]{appID,appsecret,"jsapi_ticket"});	
		if(resultList.size()>0){
			return resultList.get(0);
		}
		else
		{
			return null;
		}
	}

	@Override
	public void setApi_ticket(String jsapi_ticket, String appID, String appsecret) {
		jdbcTemplate.update("INSERT INTO wx_base (appID,appsecret,value,name) VALUES(?,?,?,?)", new Object[]{appID,appsecret,jsapi_ticket,"jsapi_ticket"});
	}
	public void updateApi_ticket(String access_token,String appID,String appsecret){
		jdbcTemplate.update("UPDATE wx_base SET value=?,update_time=NOW() WHERE appID=? AND appsecret=? and name=?", new Object[]{access_token,appID,appsecret,"jsapi_ticket"});
	} 
}
